package com.udi.project.business.collection.service.impl;

import com.udi.common.exception.base.BaseException;
import com.udi.project.business.collection.Thread.MarkDate;
import com.udi.project.business.collection.mapper.MarkMapper;
import com.udi.project.business.collection.service.CheckService;
import com.udi.project.business.utils.redis.RedisUtils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;



/**
 * @author Admin
 */
@Service
@Slf4j
public class CheckServiceImpl implements CheckService {

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private MarkMapper markMapper;

    /**
     *  对于生产线：此方法需要做实时通讯，前端需实时将扫到的码传递到后台存储
     *  WebSocket即时通讯
     *
     * @return*/
    @Override
    public List<String> check(List<String> udi) {
        List<String> ims = new ArrayList<>();
        for(String ud:udi){
            if(!redisUtils.lSet("UdiCode",ud)){
                log.info("error---------->无法向redis中存储对比数据");
                ims.add("对比器存储失败，请联系管理员");
                return ims;
            }
        }

        /** 走到这里代表采集完成 下一步 数据核对  */

        /** 获取Excel的值*/
        List<Object> stringList = redisUtils.lGet("PrintGs1",0,-1);
        System.out.println("------------->"+stringList);
        for(Object gs:stringList){
          //数据核对
          redisUtils.lRemove("UdiCode",1,gs);
        }
        List<Object>  ove = redisUtils.lGet("UdiCode",0,-1);
        if(ove == null){
            ims.add("数据核对完成，没有未采集的数据");
            log.info("数据核对完成，没有未采集的数据");
            //启动子线程持久化数据状态
            MarkDate markDate = new MarkDate();
            markDate.start();
            return ims;
        }for(Object vo:ove){
            ims.add((String) vo);
        }
        if(ims.size() != 0){
            redisUtils.del("UdiCode");
        }
        log.info("数据核对完成，有未采集的数据---->"+ims);
        return ims;

    }


    /**
    *
     * @Description: 双重检测UDI状态码并赋值
     * @return: java.lang.String
     * @Date: 2021/8/19 11:50
     */
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    @Override
    public String detectType() {
        List<String> detec = new ArrayList<>();
        List<Object> stringList = redisUtils.lGet("PrintGs1",0,-1);
        if(stringList != null){
            for(Object udi:stringList){
                detec.add((String) udi);
            }
        }else {return "没有可操作的数据";}
        List<String> noType =  markMapper.detectType(detec);
        if(noType == null){
            return "持久成功，不用忽略";
        }
        for(Object cod:stringList){
            int ove = markMapper.updateStatu((String) cod);
            if(ove == 0){
                try {
                    throw new BaseException("持久化UDI状态码失败，请检查sql语句或数据库");
                }catch (BaseException e){
                    return "持久化失败，请重试或联系管理员";
                }
            }
        }
        redisUtils.del("PrintGs1");
        return "忽略成功";
    }

}
